package com.edushare.common.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.edushare.common.constants.ConfigPropertyName;
import com.edushare.common.exceptions.EduShareRuntimeException;
import com.edushare.common.global.SystemGlobals;

/** Utility class for System Global data */
public class SystemUtils {
	
	private static Log log = LogFactory.getLog(SystemUtils.class);

	/**
	 * <p>Directory structure is like this</p>
	 * <pre>
	 * ROOT (user-provided)
	 *   `HOME_DIRECTORY(contains all directories)
	 *       |-MEDIA_DIRECTORY (for general upload purpose)
	 *       |-BACKUP_DIRECTORY (to store, create back-up files and folders)
	 *       +-TEMP_DIRECTORY (for temporary uploads, can be deleted safely)
	 * </pre>
	 * <p> All the path return Unix (/) like path separator.</p>
	 *
	 * @return root directory path, note it does NOT end with a path separator
	 */
	public static String getRootDirectoryPath() {
		String root = SystemGlobals.getInstance().getConfigProperty(ConfigPropertyName.STORAGE_ROOT_DIRECTORY);
		//remove all path separators from appended to the end of the folder name
		while(StringUtil.isValid(root) && (root.endsWith("/")||root.endsWith("\\")))
				root = root.substring(0, root.length()-2);
		return root;
	}
	
	/**
	 * <p>Directory structure is like this</p>
	 * <pre>
	 * ROOT (user-provided)
	 *   `HOME_DIRECTORY(contains all directories)
	 *       |-MEDIA_DIRECTORY (for general upload purpose)
	 *       |-BACKUP_DIRECTORY (to store, create back-up files and folders)
	 *       +-TEMP_DIRECTORY (for temporary uploads, can be deleted safely)
	 * </pre>
	 * <p> All the path return Unix (/) like path separator.</p>
	 */
	public static String getHomeDirectoryPath(boolean isRelative){
		if(!isRelative)
			return getRootDirectoryPath() + "/" + SystemGlobals.getInstance().getConfigProperty(ConfigPropertyName.STORAGE_HOME_DIRECTORY);
		else
			return SystemGlobals.getInstance().getConfigProperty(ConfigPropertyName.STORAGE_HOME_DIRECTORY);
	}

	public static String getAbsoluteHomeDirectoryPath(){
		return getHomeDirectoryPath(false);
	}
	
	/**
	 * <p>Directory structure is like this</p>
	 * <pre>
	 * ROOT (user-provided)
	 *   `HOME_DIRECTORY(contains all directories)
	 *       |-MEDIA_DIRECTORY (for general upload purpose)
	 *       |-BACKUP_DIRECTORY (to store, create back-up files and folders)
	 *       +-TEMP_DIRECTORY (for temporary uploads, can be deleted safely)
	 * </pre>
	 * <p> All the path return Unix (/) like path separator.</p>
	 */
	public static String getMediaDirectoryPath(boolean isRelative){
		if(!isRelative)//absolute
			return getAbsoluteHomeDirectoryPath() + "/" + SystemGlobals.getInstance().getConfigProperty(ConfigPropertyName.STORAGE_MEDIA_DIRECTORY); 
		else //relative
			return SystemGlobals.getInstance().getConfigProperty(ConfigPropertyName.STORAGE_HOME_DIRECTORY) + "/" + SystemGlobals.getInstance().getConfigProperty(ConfigPropertyName.STORAGE_MEDIA_DIRECTORY);
	}
	
	/**
	 * <p>Directory structure is like this</p>
	 * <pre>
	 * ROOT (user-provided)
	 *   `HOME_DIRECTORY(contains all directories)
	 *       |-MEDIA_DIRECTORY (for general upload purpose)
	 *       |-BACKUP_DIRECTORY (to store, create back-up files and folders)
	 *       +-TEMP_DIRECTORY (for temporary uploads, can be deleted safely)
	 * </pre>
	 * <p> All the path return Unix (/) like path separator.</p>
	 */
	public static String getBackupDirectoryPath(boolean isRelative){
		if(!isRelative) //absolute
			return getAbsoluteHomeDirectoryPath() + "/" + SystemGlobals.getInstance().getConfigProperty(ConfigPropertyName.STORAGE_BACKUP_DIRECTORY); 
		else //relative
			return SystemGlobals.getInstance().getConfigProperty(ConfigPropertyName.STORAGE_HOME_DIRECTORY) + "/" + SystemGlobals.getInstance().getConfigProperty(ConfigPropertyName.STORAGE_BACKUP_DIRECTORY);
	}
	
	/**
	 * <p>Directory structure is like this</p>
	 * <pre>
	 * ROOT (user-provided)
	 *   `HOME_DIRECTORY(contains all directories)
	 *       |-MEDIA_DIRECTORY (for general upload purpose)
	 *       |-BACKUP_DIRECTORY (to store, create back-up files and folders)
	 *       +-TEMP_DIRECTORY (for temporary uploads, can be deleted safely)
	 * </pre>
	 * <p> All the path return Unix (/) like path separator.</p>
	 */
	public static String getTempDirectoryPath(boolean isRelative){
		if(!isRelative) //absolute
			return getAbsoluteHomeDirectoryPath() + "/" + SystemGlobals.getInstance().getConfigProperty(ConfigPropertyName.STORAGE_TEMP_DIRECTORY); 
		else //relative
			return SystemGlobals.getInstance().getConfigProperty(ConfigPropertyName.STORAGE_HOME_DIRECTORY) + "/" + SystemGlobals.getInstance().getConfigProperty(ConfigPropertyName.STORAGE_TEMP_DIRECTORY);
	}
	
	/**
	 * <p>
	 * The standard way to store path of a file in DB is relative path. Relative to STORAGE_ROOT_DIRECTORY. 
	 * Ideally, the relative path should not have any leading "/" or "\". If there is any, this method handles that.
	 * </p>
	 * @param relativePath
	 * @return
	 */
	public static String getAbsolutePathFromRelative(String relativePath){
		if(!StringUtil.isValid(relativePath)){
			log.error("Relative path is illegal: " + relativePath);
			throw new EduShareRuntimeException("Relative path is illegal: " + relativePath);
		}
		
		while(relativePath.startsWith("/") || relativePath.startsWith("\\"))
			relativePath = relativePath.substring(1);
		
		return getRootDirectoryPath() + "/" + relativePath;
	}
	
}